/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sfs2x.extension.fourinarow.main;

import com.athena.objects.Game;
import com.athena.objects.Player;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

/**
 *
 * @author dodinhchien
 */
public class LeaveHandle extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject params) {

        FourinarowExtension extension = (FourinarowExtension) getParentExtension();
        Player player = extension.getUsersList().get(user.getId());
        if (player.getRoomId() != -1) {
            Game game = extension.getGamesList().get(player.getRoomId());
            if (game != null && !game.isStarted()) {
                getApi().leaveRoom(user, game.getRoom());
                game.getPlayers().remove(user.getId());
            }
        } // User can be only in 1 room at a time

    }

}
